package com.zhanglijie.theory.threadpermessage;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/8/14 0014 19:42
 */
public class MyServer {
    private int port;
    public MyServer(int port){
        this.port = port;
    }

    public void start() throws IOException {
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("start listen on" + port);
        try {
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("connected to server");
                new Thread(() -> {
                    try {
                        Service.service(socket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        }finally {
            serverSocket.close();
        }
    }
}
